window.onload = () => {
    var userRole = ["ALL", "USER", "ADMIN"];
    let tabs = document.querySelectorAll("#main > div > div.setting.pb10 > div.nav-tabs.pa h");
    let boxes = document.querySelectorAll(".setting-wrap");
    for(let i = 0; i < tabs.length; i++) {
        tabs[i].onclick = () => {
            for(let j = 0; j < tabs.length; j++) {
                tabs[j].classList.remove("active");
                boxes[j].style.display = "none";
            }
            tabs[i].classList.add("active");
            boxes[i].style.display = "block";
        };
    }
    if($.cookie("uid") != null) {
        $.ajax({
            url: "/bookcity/user/getUser",
            type: "post",
            data: {
                "uid": $.cookie("uid")
            },
            success: function (data) {
                data = JSON.parse(data);
                if(data.status == 0) {  // 成功
                    // console.log(data);
                    $("#js-portrait").attr("src", data.user.portrait);
                    $("#js-uname").attr("value", data.user.uname);
                    $("#urole").attr("value", userRole[data.user.urole]);
                    $("#signature").text(data.user.signatrue);
                    $("#uid").attr("value", data.user.uid);
                    $("#passwd").attr("value", data.user.upasswd);
                    $("#uemail").attr("value", data.user.uemail);
                    $("#uphone").attr("value", data.user.uphone);
                } else {
                    $("#js-portrait").attr("src", "");
                    $("#js-uname").attr("value", "");
                    $("#urole").attr("value", "");
                    $("#signature").text("");
                    $("#uid").attr("value", "");
                    $("#passwd").attr("value", "");
                    $("#uemail").attr("value", "");
                    $("#uphone").attr("value", "");
                    alert("用户不存在！");
                }
            }
        });
    } else {
        alert("请先登录！");
        $.removeCookie('uid' , { path: '/' });
        $.removeCookie('uname' , { path: '/' });
        $.removeCookie('isUser' , { path: '/' });
        window.location.href = "/bookcity/";
    }
    function updateUser() {
        let name = $("#js-uname").val();
        let role = $("#urole").val();
        let signature = $("#signature").val();
        let passwd = $("#passwd").val();
        let email = $("#uemail").val();
        let phone = $("#uphone").val();
        $.ajax({
            url: "/bookcity/userSL/updateUser",
            type: "post",
            data: {
                "uid": $.cookie("uid"),
                "uname": name,
                "urole": role,
                "signature": signature,
                "upasswd": passwd,
                "uemail": email,
                "uphone": phone
            },
            success: function (data) {
                console.log(data);
                if(data == true) {
                    alert("修改成功");
                } else {
                    alert("修改失败");
                }
            }
        })
    }
    $("#base-submit").click(updateUser);

    $("#profile-submit").click(updateUser);
};